localarch := $(shell uname -m)
ARCH := unknown
DownloadURL := ""
binfile := ""
tarballname := Cangjie-0.53.18-linux_$(ARCH).tar.gz
ifeq ($(localarch), x86_64)
    ARCH := x64
	DownloadURL := "https://cangjie-lang.cn/v1/files/auth/downLoad?nsId=142267&fileName=Cangjie-0.53.18-linux_x64.tar.gz&objectKey=67a2c8976a75297d1cdc1d4c"
else
    ARCH := aarch64
	DownloadURL := "https://cangjie-lang.cn/v1/files/auth/downLoad?nsId=142267&fileName=Cangjie-0.53.18-linux_aarch64.tar.gz&objectKey=67a2c8606a75297d1cdc1d48"
endif

.PHONY: envset
envset: 
	@echo $(ARCH)
	@echo $(PWD)
	@echo  Cangjie-0.53.18-linux_$(ARCH).tar.gz
	@echo Dealing : $(DownloadURL)
	wget -O Cangjie-0.53.18-linux_$(ARCH).tar.gz $(DownloadURL)
	tar -xvf Cangjie-0.53.18-linux_$(ARCH).tar.gz
	@echo $(PWD)
	ls $(PWD)
	# mv cangjie ~/.cangjieSDK
	# source ~/.cangjieSDK/envsetup.sh
	# source cangjie/envsetup.sh
	@echo "TEST ENV"
	# cjc -v
	@echo "TEST DONE"
	@echo "ENV FIX"
	# check if the /usr/lib64/crtbeginS.o exists if not exist do the command below
	if [ ! -f /usr/lib64/crtbeginS.o ]; then sudo ln -s /usr/lib/gcc/x86_64-openEuler-linux/12/crtbeginS.o /usr/lib64/crtbeginS.o; fi
	if [ ! -f /usr/lib64/crtendS.o ]; then sudo ln -s /usr/lib/gcc/x86_64-openEuler-linux/12/crtendS.o /usr/lib64/crtendS.o; fi
	@echo "ENV FIX DONE"

build:
	@echo "Building filesorter"
	cjpm build

install: 
	mkdir -p $(DESTDIR)/usr/bin
	install -m 0755 target/release/bin/main $(DESTDIR)/usr/bin/filesorter
	alias main="filesorter"

clean: 
	cjpm clean
	rm -rf ~/.cangjieSDK
	rm -rf Cangjie-0.53.18-linux_$(ARCH).tar.gz

